
package com.yu.interceptor;

import javax.servlet.http.HttpSession;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.yu.model.Manufacturer;
import com.yu.model.Order;
import com.yu.model.User;
import com.yu.util.CookiesUtil;
import com.yu.util.StringUtil;

public class UserInterceptor implements Interceptor {

    public void intercept(Invocation inv) {
        Controller controller = inv.getController();
        HttpSession session = controller.getSession();
        if (session.getAttribute("user") != null){
        	User user = (User) session.getAttribute("user");
        	controller.setAttr("user", user);
        	inv.invoke();
        } else {
        	
        	String wxid = CookiesUtil.getName(controller.getRequest(), "wxid");
        	if (!StringUtil.isEmpty(wxid)){
        		User user = User.dao.findFirst("select * from w_user where wxid = '" + wxid +"'");
            	session.setAttribute("user",user);
            	controller.setAttr("user", user);
            	inv.invoke();
        	} else {
        		String message = "请先登录再进行操作！";
                controller.setAttr("message", message);
                String redirectionUrl = controller.getRequest().getContextPath() + "/";
                controller.setAttr("redirectionUrl", redirectionUrl);
                controller.render("/WEB-INF/view/index/login.html");
        	}
        }
    }
}
